<?php
class Lib_Request_Sign{

    public static function build_data($data){
        if(empty($data['method']) || empty($data['requestId']) || empty($data['timestamp']) || empty($data['parameters']) || empty($data['signKey'])){
            return false;
        }
        extract($data);
        return "method{$method}requestId{$requestId}timestamp{$timestamp}parameters{$parameters}{$signKey}";
    }

    public static function sign($data)
    {
        $str = self::build_data($data);
        if(!$str){
            return false;
        }

        return bin2hex(hash("sha256",$str,true));
    }

    public static function verify($data, $sign_p){
        $str = self::build_data($data);
        if(!$str){
            return false;
        }
        $sign_c = bin2hex(hash("sha256",$str,true));
        return $sign_c == $sign_p;
    }
}